#pragma once

#include <iostream>
#include <pthread.h>
#include <queue>

template<typename T>
class BlockQueue
{
    static const int _defaultnum = 20;

public:
    BlockQueue(int maxcap = _defaultnum)
        : _maxcap(maxcap)
    {
        pthread_mutex_init(&_mutex, nullptr);
        pthread_cond_init(&_p_cond, nullptr);
        pthread_cond_init(&_c_cond, nullptr);
    }

    T pop()
    {
        pthread_mutex_lock(&_mutex);
        while (_q.size() == 0)
        {
            pthread_cond_wait(&_c_cond, &_mutex);
        }

        T out = _q.front();
        _q.pop();

        pthread_cond_signal(&_p_cond);
        pthread_mutex_unlock(&_mutex);

        return out;
    }

    void push(const T& in)
    {
        pthread_mutex_lock(&_mutex);
        while (_q.size() == _maxcap)
        {
            pthread_cond_wait(&_p_cond, &_mutex);
        }

        _q.push(in);

        pthread_cond_signal(&_c_cond);
        pthread_mutex_unlock(&_mutex);
    }

    ~BlockQueue()
    {
        pthread_mutex_destroy(&_mutex);
        pthread_cond_destroy(&_p_cond);
        pthread_cond_destroy(&_c_cond);
    }

private:
    std::queue<T> _q;
    int _maxcap;

    pthread_mutex_t _mutex;
    pthread_cond_t _p_cond;
    pthread_cond_t _c_cond;
};

